/*
 * ******************************************************************************
 *   Copyright (c) 2013-2014 Gabriele Mariotti.
 *
 *   Licensed under the Apache License, Version 2.0 (the "License");
 *   you may not use this file except in compliance with the License.
 *   You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 *   Unless required by applicable law or agreed to in writing, software
 *   distributed under the License is distributed on an "AS IS" BASIS,
 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *   See the License for the specific language governing permissions and
 *   limitations under the License.
 *  *****************************************************************************
 */

package it.gmariotti.cardslib.demo.fragment.nativeview;

import it.gmariotti.cardslib.demo.ResourceTable;
import it.gmariotti.cardslib.demo.fragment.BaseMaterialFragment;
import it.gmariotti.cardslib.library.internal.Card;
import it.gmariotti.cardslib.library.internal.CardGridArrayAdapter;
import it.gmariotti.cardslib.library.internal.CardHeader;
import it.gmariotti.cardslib.library.internal.CardThumbnail;
import it.gmariotti.cardslib.library.view.CardGridView;
import ohos.aafwk.content.Intent;
import ohos.agp.components.*;
import ohos.app.Context;

import java.util.ArrayList;

/**
 * Grid as Google Play example
 *
 * @author Gabriele Mariotti (gabri.mariotti@gmail.com)
 */
public class NativeGridGplayFragment extends BaseMaterialFragment {

    protected ScrollView mScrollView;

    @Override
    protected int getSubTitleHeaderResourceId() {
        return ResourceTable.String_header_title_subtitle_ggplay;
    }

    @Override
    protected int getTitleHeaderResourceId() {
        return ResourceTable.String_header_title_group3;
    }

    @Override
    protected String getDocUrl() {
        return "";
    }

    @Override
    protected String getSourceUrl() {
        return "";
    }

    @Override
    protected void onStart(Intent intent) {
        super.onStart(intent);
        setUIContent(ResourceTable.Layout_demo_fragment_native_grid_gplay);
        initCards();
    }


    private void initCards() {

        ArrayList<Card> cards = new ArrayList<Card>();
        for (int i = 0; i < 200; i++) {

            GplayGridCard card = new GplayGridCard(this);

            card.headerTitle = "App example " + i;
            card.secondaryTitle = "Some text here " + i;
            card.rating = (float) (Math.random() * (5.0));

            //Only for test, change some icons
            if ((i % 6 == 0)) {
                card.resourceIdThumbnail = ResourceTable.Media_ic_ic_dh_bat;
            } else if ((i % 6 == 1)) {
                card.resourceIdThumbnail = ResourceTable.Media_ic_ic_dh_net;
            } else if ((i % 6 == 2)) {
                card.resourceIdThumbnail = ResourceTable.Media_ic_tris;
            } else if ((i % 6 == 3)) {
                card.resourceIdThumbnail = ResourceTable.Media_ic_info;
            } else if ((i % 6 == 4)) {
                card.resourceIdThumbnail = ResourceTable.Media_ic_smile;
            }

            card.init();
            cards.add(card);
        }

        CardGridArrayAdapter mCardArrayAdapter = new CardGridArrayAdapter(this, cards);

        CardGridView listView = (CardGridView) this.findComponentById(ResourceTable.Id_carddemo_grid_base1);
        if (listView != null) {
            listView.setAdapter(mCardArrayAdapter);
        }
    }


    public class GplayGridCard extends Card {

        protected Text mTitle;
        protected Text mSecondaryTitle;
        protected Rating mRatingBar;
        protected int resourceIdThumbnail = -1;
        protected int count;

        protected String headerTitle;
        protected String secondaryTitle;
        protected float rating;

        public GplayGridCard(Context context) {
            super(context, ResourceTable.Layout_carddemo_native_gplay_inner_content);
        }

        public GplayGridCard(Context context, int innerLayout) {
            super(context, innerLayout);
        }

        private void init() {
            CardHeader header = new CardHeader(getContext(), ResourceTable.Layout_native_inner_gplay_header);
            header.setButtonOverflowVisible(true);
            header.setTitle(headerTitle);
            //todo
//            header.setPopupMenu(R.menu.popupmain, new CardHeader.OnClickCardHeaderPopupMenuListener() {
//                @Override
//                public void onMenuItemClick(BaseCard card, MenuItem item) {
//                    Toast.makeText(getContext(), "Item " + item.getTitle(), Toast.LENGTH_SHORT).show();
//                }
//            });

            addCardHeader(header);

            GplayGridThumb thumbnail = new GplayGridThumb(getContext());
            if (resourceIdThumbnail > -1)
                thumbnail.setDrawableResource(resourceIdThumbnail);
            else
                thumbnail.setDrawableResource(ResourceTable.Media_ic_ic_launcher_web);
            addCardThumbnail(thumbnail);
        }

        @Override
        public void setupInnerViewElements(ComponentContainer parent, Component view) {

            Text title = (Text) view.findComponentById(ResourceTable.Id_carddemo_gplay_main_inner_title);
            title.setText("FREE");

            Text subtitle = (Text) view.findComponentById(ResourceTable.Id_carddemo_gplay_main_inner_subtitle);
            subtitle.setText(secondaryTitle);

            Rating mRatingBar = (Rating) parent.findComponentById(ResourceTable.Id_carddemo_gplay_main_inner_ratingBar);

            mRatingBar.setRatingItems(5);
            mRatingBar.setMaxValue(5);
            mRatingBar.setGrainSize(0.5f);
            mRatingBar.setScore(rating);
        }

        class GplayGridThumb extends CardThumbnail {

            public GplayGridThumb(Context context) {
                super(context);
            }

            @Override
            public void setupInnerViewElements(ComponentContainer parent, Component viewImage) {
            }
        }

    }

}
